﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using E_Commerce.Domain.Interfaces;
using Dapper;
using Microsoft.Extensions.Configuration;
using E_Commerce.Domain.Entities;//引用

namespace E_Commerce.Infrastructure.Data
{
   public class SpecificationRepository:DapperRepository<Specification>,ISpecificationRepository
    {
        /// <summary>
        /// 依赖注入
        /// </summary>
        private IConfiguration _configuration;
        public SpecificationRepository(IConfiguration configuration)
        {
            this._configuration = configuration;
            this.ConnStr = _configuration.GetConnectionString("ConnStr");
            SimpleCRUD.SetDialect(SimpleCRUD.Dialect.MySQL);
            Conn = new MySql.Data.MySqlClient.MySqlConnection(this.ConnStr);
        }
        /// <summary>
        /// 添加商品规格
        /// </summary>
        /// <param name="s"></param>
        /// <returns></returns>

        public async Task<int> AddSpecAsyn(Specification s)
        {
            try
            {
                return (int)await Conn.InsertAsync(s);
            }
            catch (Exception ex)
            {

                throw new Exception(ex.Message);
            }
            
        }
        /// <summary>
        /// 删除商品规格
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task<int> DeleSpecAsync(int id)
        {
            string sql = $"delete from Specification where SpeciId={id}";
            return await Conn.ExecuteAsync(sql);
        }
        /// <summary>
        /// 反填规格
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>

        public Specification FillSpecAsync(int id)
        {
            return Conn.Get<Specification>(id);
        }
        /// <summary>
        /// 显示商品规格
        /// </summary>
        /// <returns></returns>
        public List<Specification> GetSpecifications(int id)
        {
            string sql = $"select * from Specification where proId={id}";
            return (List<Specification>)Conn.Query<Specification>(sql);
        }
        /// <summary>
        /// 修改规格
        /// </summary>
        /// <param name="s"></param>
        /// <returns></returns>
        public async Task<int> UptSpecAsync(Specification s)
        {
            try
            {
                return await Conn.UpdateAsync(s);
            }
            catch (Exception ex)
            {

                throw new Exception(ex.Message);
            }
            
        }
    }
}
